var Project = new Class({
	current:'summary',
	list:false,
	
	initialize:function() {
		
	},
	
	getList:function(collection) {
		var _this = this;
		if($defined(j.user.user_id)) {
			j.request('getProjects','',function (res) {
				_this.list = JSON.decode(res);
				j.BC.broadcastMessage('project.loaded');
			});
			
		} else {
			user.logout();
		}
	},
		
		
	create:function(title) {
		if(title !== '') {
			$('create_project_response').innerHTML = 'Creating project ...';
			j.request('project_create',{title:title,collection:currentcollection.id},function (response) {
				response = JSON.decode(response);		
				if(response.result == "success") {
					BC.broadcastMessage('modal.close');
					projects.push(response.project);
					project_select(response.project.project_id);
					
				} else {
					$('create_project_response').innerHTML = response.reason;
				}
			});
		} else {
			$('create_project_response').innerHTML = 'Please enter a Project name';
		}
	},


	/**
	 * Creates a new project
	 * 
	 * This function raises a promt window, if a string is entered a new
	 * project with a name like the given string will be created.
	 * 
	 * @param key Key Code of the key to be checked
	 * @return bool true or false
	 */
	create_show:function() {
		var floater = new Modal({
			windowtitle:'Create new Project',
			content:templates.new_timesheet.process()
		});
		floater.div.addEvent('keyup', function(ev) { if(ev.key=='enter') { project_create($('newProject_title').value); }});
		$('newproject_submit').addEvent('click', function () { project_create($('newProject_title').value); });
		$('newProject_title').focus();
	},
	
	/**
	 * Deletes a Project including all Tasks. This function calls the Server
	 * Method "deleteProject" in Class "project" and passes the parameter "id" to it.
	 * 
	 * @param id - Projects ID
	 */
	delete_show:function(id) {
		var floating = new Modal({
			windowtitle:'Delete Project',
			content:'Do you really want to delete this project and all its related tasks? Please be aware that other user might still be working on it.<br><br><br><div class="button" id="delProject_yes"> yes </div> <div class="button" id="delProject_no"> cancel </div><br><br>'
		});
		
		$('delProject_yes').onclick=function() {
			j.request('project_delete',{'id':id},function(obj) {
		  		floating.close();
				projects.each(function(e,i) { if(e.project_id == id) { delete(projects[i]); }});
				BC.broadcastMessage('project.delete',id);
				if(id == currentProject) { currentProject = false; }
				return;
			});
		};
		
		$('delProject_no').onclick=function() {
			floating.close();
		};
	},
	
	events:function(func, param) {
		
		/*
		if(func == 'collection.select') {
			this.getList(param);
		}
		*/
	}
});
